function smb_MessageBoxShow(title, content, buttons, callback) {
    appcan.window.alert({
        title : title,
        content : content,
        buttons : buttons,
        callback : callback
    });
}

/*
 * 获取最近打开窗口的名称
 */
function smb_GetLatestOpenedWindow() {
    var jsonArray = appcan.locStorage.getVal("$OpenedPages");
    var pages = undefined;
    if (jsonArray)
        pages = JSON.parse(jsonArray);
    else
        pages = [];
    if (pages.length > 0) {
        return pages[pages.length - 1];
    } else
        return undefined;
}

/*
 * 清理打开页面时传递的参数
 */
function smb_ClearWindowParams(name) {
    var paramKey = "Params-" + name;
    var paramNames = smb_GetStorageArray(paramKey);
    for (var i = 0; i < paramNames.length; i++) {
        smb_ClearStorageKey(paramKey + "-" + paramNames[i]);
    }
    smb_ClearStorageKey(paramKey);
}

/*
 * 获取页面参数具体项值
 */
function smb_GetWindowParam(pageName, paramName) {
    if (!paramName)
        paramName = pageName;
    else
        pageName = smb_GetLatestOpenedWindow();
    var paramKey = "Params-" + pageName;
    var sStr = appcan.locStorage.getVal(paramKey + "-" + paramName);
    return sStr;
}

/*缓存登录验证后继续打开的页面名称*/
function smb_CachePrepareWindow(name, data) {
    smb_ClearStorageKey("$PrepareWindow");
    smb_CacheStorageVal("$PrepareWindow", {
        name : name,
        url : data
    });
    return false;
}

/*打开登录验证后继续打开的页面*/
function smb_OpenPrepareWindow() {
    var sJson = appcan.locStorage.getVal("$PrepareWindow");
    if (sJson) {
        var jsonObj = JSON.parse(sJson);
        var params = smb_GetWindowParams(jsonObj.name);
        smb_OpenWindowOrigin(true, jsonObj.name, jsonObj.url, params);
    }
    smb_ClearStorageKey("$PrepareWindow");
}

function smb_ClearPrepareWindowParams() {
    var sJson = appcan.locStorage.getVal("$PrepareWindow");
    if (sJson) {
        var jsonObj = JSON.parse(sJson);
        smb_ClearWindowParams(jsonObj.name);
    }
    smb_ClearStorageKey("$PrepareWindow");
}

/*
 * 获取打开该页时所传递的所有参数对象
 */
function smb_GetWindowParams(pageName) {
    if (!pageName)
        pageName = smb_GetLatestOpenedWindow();
    var paramKey = "Params-" + pageName;
    var sStr = appcan.locStorage.getVal(paramKey);
    var params = {};
    if (sStr) {
        var arrParam = JSON.parse(sStr);
        for (var i = 0; i < arrParam.length; i++) {
            var paramName = arrParam[i];
            var val = appcan.locStorage.getVal(paramKey + "-" + paramName);
            params[paramName] = val;
        }
    }
    return params;
}

/*
 * 缓存打开页面时传递的参数
 */
function smb_CacheWindowParams(name, params) {
    var paramKey = "Params-" + name;
    for (var p in params) {
        var param = params[p];
        smb_PushStack(paramKey, p);
        smb_CacheStorageVal(paramKey + "-" + p, param);
    }
}

function smb_OpenWindow(name, url, params, aniId) {
    if (!aniId)
        aniId = 10;
    var randomStr = smb_RandomString(6);
    //alert(name + "--" + url);

    var _openPageName = name + '_' + randomStr;
    smb_OpenWindowOrigin(_openPageName, url, params, aniId);
}

function smb_OpenWindowOrigin(name, url, params, aniId) {
    if (!aniId)
        aniId = 10;
    //alert(name + "--" + url);

    params = params || {};
    params = $.extend({}, params, {
        CurrentPageName : name
    });
    //
    smb_ClearWindowParams(name);
    //清空/传递参数  使用smb_GetWindowParam获取参数值
    smb_CacheWindowParams(name, params);
    setTimeout(function() {
        appcan.window.open({
            name : name,
            data : url,
            aniId : aniId
        });
        var lastPage = smb_GetLatestOpenedWindow();
        if (lastPage !== name) {
            smb_PushStack("$OpenedPages", name);
            smb_CacheStorageKey("$OpenedPages");
        }
    }, 150);
}

/*关闭最近使用smb_OpenWindow打开的窗口*/
function smb_CloseWindow(aniId, timeout, cbBeforeClose) {
    var name = smb_PopStack("$OpenedPages");
    smb_ClearWindowParams(name);
    if (cbBeforeClose)
        cbBeforeClose();
    if (!aniId)
        aniId = -1;
    if (!timeout)
        timeout = 200;
    //timeout:动画持续时间
    appcan.window.close(aniId, timeout);
}

//关闭当前页并打开新的页
function smb_ReplaceCurrentWindow(name, url, params, aniId) {
    var currentName = smb_PopStack("$OpenedPages");
    smb_ClearWindowParams(currentName);
    if (!aniId)
        aniId = -1;
    smb_OpenWindow(name, url, params, aniId);
    //timeout:动画持续时间
    appcan.window.close(aniId, 200);
}

//订阅一个频道
//Subscribe Channels
function smb_SubscribeMessage(channelId, callback) {
    appcan.window.subscribe(channelId, callback);
    if (channelId) {
        smb_InsertIntoArray("SubscribeChannels", channelId);
    }
    smb_CacheStorageKey("SubscribeChannels");
}

//向指定通道发送消息
function smb_PublishMessage(channelId, msg) {
    appcan.window.publish(channelId, msg);
}

//滚动
function smb_Scroll(wapper, options) {
    var scrollCall,
        scrollStartCall,
        scrollEndCall;
    if (options) {
        scrollStartCall = options.scrollStartCall;
        scrollCall = options.scrollCall;
        scrollEndCall = options.scrollEndCall;
    } else {
        options = {
            scrollBarY : true,
            scrollBarX : false
        };
    }
    var params = $.extend({}, {
        scrollBarFade : true
    }, options);
    var myScroll = new JRoll(wapper, params);

    document.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, false);
    myScroll.on('scrollStart', function() {// 滚动结束
        if (scrollStartCall)
            scrollStartCall(this);
    });
    myScroll.on('scroll', function() {// 滚动进行时
        if (scrollCall)
            scrollCall(this);
    });
    myScroll.on('scrollEnd', function() {// 滚动结束
        if (scrollEndCall)
            scrollEndCall(this);
    });
    return myScroll;
}